package com.money.tracker.auth.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.money.tracker.auth.entity.SysRole;
import com.money.tracker.auth.mapper.SysRoleMapper;
import com.money.tracker.auth.service.ISysRoleService;
import com.money.tracker.auth.service.ISysUserRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色信息表 服务实现类
 * </p>
 *
 * @author zhangshuai
 * @since 2024-09-28
 */
@Service
@RequiredArgsConstructor
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    private final ISysUserRoleService userRoleService;

    @Override
    public Set<String> getRolePermissionByUserId(Long userId) {
        // 获取用户角色对应关系
        List<Long> userRoles = userRoleService.listByUserId(userId);
        if (CollectionUtil.isEmpty(userRoles)){
            return Collections.emptySet();
        }
        // 获取角色权限
        LambdaQueryWrapper<SysRole> sysRoleLambdaQueryWrapper = new LambdaQueryWrapper<SysRole>()
                .in(SysRole::getId, userRoles);
        return this.list(sysRoleLambdaQueryWrapper)
                .stream()
                .map(SysRole::getRoleKey)
                .collect(Collectors.toSet());
    }
}
